package com.example.java_second_hand_market;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.sql.*;


public class PaymentDialog extends Stage {
    String DBUrl=ParameterDB.ParaDBUserUrl;
    String DBUser="root", DBPassword="123456";
    @FXML
    Label label=new Label("确认支付？");
    @FXML
    Button quit=new Button("退出");
    @FXML
    Button pay =new Button("付款");
    Buyer bye;
    Item ite;
    public PaymentDialog(Buyer by,Item it,int width,int height){
        bye=by;
        ite=it;
        quit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                close();
            }
        });
        pay.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                try {
                    Connection con= DriverManager.getConnection(DBUrl,DBUser,DBPassword);
                    Statement stmt=con.createStatement();
                    stmt.executeUpdate("UPDATE item_list SET buyer = '"+bye.getName()+"',is_sold = 1 where ID = "+ite.getInfoId());
                    CommonDialog.InformationDialog("支付完成");
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        HBox hb2=new HBox();
        hb2.getChildren().addAll(quit,pay);
        hb2.setMargin(quit,new Insets(3,0,0,0));
        hb2.setMargin(pay,new Insets(3,0,0,0));

        BorderPane bp=new BorderPane();
        bp.setCenter(label);
        bp.setBottom(hb2);

        setX(820);
        setY(500);
        initModality(Modality.APPLICATION_MODAL);
        setScene(new Scene(bp));

        setWidth(700);
        setHeight(500);
        setX((width-getWidth()) / 2);
        setY((height-getHeight())/ 2);
        setTitle("确认支付");
        show();
    }
}
